﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace K3R.Generics {
	[DataContract]
	public class Page<T> : List<T> {
		#region Properties
		[DataMember(Name = "pageIndex")] public int PageIndex { get; set; }
		[DataMember(Name = "pageSize")] public int PageSize { get; set; }
		#endregion

		#region Indexers
		public T this[int index, int pageIndex, int pageSize] {
			get {
				return base[Page<T>.GetIndex(pageIndex, pageSize, index)];
			}
			set {
				base[Page<T>.GetIndex(pageIndex, pageSize, index)] = value;
			}
		}

		public T this[int index, int pageIndex] {
			get {
				return this[index, pageIndex, PageSize];
			}
			set {
				this[index, pageIndex, PageSize] = value;
			}
		}

		public new T this[int index] {
			get {
				return this[index, PageIndex, PageSize];
			}
			set {
				this[index, PageIndex, PageSize] = value;
			}
		}
		#endregion

		#region Page
		public List<T> GetPage(int pageIndex, int pageSize) {
			return this.GetRange((pageSize * PageIndex), pageSize);
		}

		public List<T> GetPage(int pageIndex) {
			return this.GetPage(pageIndex, PageSize);
		}

		public List<T> GetPage() {
			return this.GetPage(PageIndex);
		}
		#endregion

		#region Helpers
		public static int GetIndex(int pageIndex, int pageSize, int index) {
			return (pageSize * pageIndex) + index;
		}
		#endregion
	}
}
